package com.sptpc.qimoxiangmu.service;


import com.sptpc.qimoxiangmu.model.Type;
import com.sptpc.qimoxiangmu.model.User;
import com.sptpc.qimoxiangmu.model.vo.TypeVo;
import com.sptpc.qimoxiangmu.model.vo.UserVo;
import com.sptpc.qimoxiangmu.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    //user的插入
    public User saveUser(String user,String password){
        return userRepository.save(User.builder().user(user).password(password).build());
    }

    //通过表单对象更新，前端
    public User update(UserVo userVo){
        Optional<User> oldUser=userRepository.findById(userVo.getId());
        if (oldUser.isPresent()){
            User category=oldUser.get();
            category.setUser(userVo.getUser());
            category.setPassword(userVo.getPassword());
            return userRepository.saveAndFlush(category);
        }else{
            return null;
        }
    }
}
